/*
 * Copyright 2015 Zhongan.com All right reserved. This software is the
 * confidential and proprietary information of Zhongan.com ("Confidential
 * Information"). You shall not disclose such Confidential Information and shall
 * use it only in accordance with the terms of the license agreement you entered
 * into with Zhongan.com.
 */
package [basepackage].web;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import lombok.extern.slf4j.Slf4j;
import [basepackage].bean.Customer;
import [basepackage].service.FeignService;
import [basepackage].service.TestService;
import net.wicp.tams.common.connector.beans.CusDynaBean;
import net.wicp.tams.common.connector.executor.IConfigManager;
import net.wicp.tams.common.connector.executor.impl.CommonService;
import net.wicp.tams.common.http.HttpClient;
import net.wicp.tams.common.http.HttpResult;

/***
 * 测试用的Controller
 * 
 * @author zhoujunhui
 *
 */
@Controller
@RestController
@Configuration
@RequestMapping(value = { "/test" })
@Slf4j
public class TestController {
	@Autowired
	private CommonService commonService;
	@Autowired
	public IConfigManager configmanager;

	@Autowired
	private TestService testService;

	@Autowired
	private DiscoveryClient discoveryClient;
	@Autowired
	private FeignService feignService;


	// cache的使用
	@RequestMapping(value = "/get", method = RequestMethod.GET)
	String get() {
		Customer cus = testService.get("zhangsan");
		log.info("name={}", cus.getName());
		return "1";
	}

	// cache的使用2
	@RequestMapping(value = "/save", method = RequestMethod.GET)
	String save() {
		Customer temp = new Customer("zhangsan", 82, new Date());
		testService.save(temp);
		return "1";
	}

	// metrics调用日志
	@RequestMapping(value = "/metrics", method = RequestMethod.GET)
	String metrics() {
		testService.callCount();
		return "aaa";
	}

	@RequestMapping(value = "/callori", method = RequestMethod.GET)
	String callRemoteService() {
		List<ServiceInstance> list = discoveryClient.getInstances("DEMO-MICRO");
		if (list != null && list.size() > 0) {
			String str = list.get(0).getUri().toString() + "/exinfo";
			HttpResult ret = HttpClient.doGet(str);
			return ret.getBodyStr();
		}
		return "no";
	}

	@RequestMapping(value = "/rest", method = RequestMethod.GET)
	String callRemoteService2() {
		String postForObject = testService.restTemplate();
		return postForObject;
	}

	@RequestMapping(value = "/callfeign2", method = RequestMethod.GET)
	String callRemoteService3() {
		return feignService.timeout();
	}

	// 使用动态Bean调接口,请参考:/connector 路径调用
	@RequestMapping(value = "/test", method = RequestMethod.GET)
	public String showDynaBean() {
		String requestBody = "{\"transactionNo\":\"A2440101\",\"ControlInfo\":{\"requestCommand\":\"test\",\"senderSystem\":\"IV\",\"senderApplication\":\"Hammer\",\"version\":\"1.0\",\"senderChannel\":\"H5\",\"msgId\":\"1493708268936\"}}";
		JSONObject params = JSONObject.parseObject(requestBody);
		CusDynaBean inputBean = configmanager.getInputBeanInputBody(params);
		CusDynaBean resp = commonService.exe(inputBean);
		log.info(resp.getStrValueByName("retvalue"));
		return "aaa";
	}
}